package com.example.alarmclock;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class AlarmModel {
    private int hour;
    private int minute;
    private String label;
    private String repeat;
    private boolean vibrate;
    private long id;

    public AlarmModel() {
        this.hour = 0;
        this.minute = 0;
        this.label = "";
        this.repeat = "无";
        this.vibrate = true;
        this.id = System.currentTimeMillis(); // Unique ID based on current time
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getRepeat() {
        return repeat;
    }

    public void setRepeat(String repeat) {
        this.repeat = repeat;
    }

    public boolean isVibrate() {
        return vibrate;
    }

    public void setVibrate(boolean vibrate) {
        this.vibrate = vibrate;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        if (calendar.before(Calendar.getInstance())) {
            calendar.add(Calendar.DATE, 1);
        }

        return calendar.getTimeInMillis();
    }

    public String getTimeString() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        return sdf.format(calendar.getTime());
    }

    @Override
    public String toString() {
        return hour + "," + minute + "," + label + "," + repeat + "," + vibrate + "," + id;
    }

    public static AlarmModel fromString(String str) {
        String[] parts = str.split(",");
        AlarmModel alarm = new AlarmModel();
        alarm.setHour(Integer.parseInt(parts[0]));
        alarm.setMinute(Integer.parseInt(parts[1]));
        alarm.setLabel(parts[2]);
        alarm.setRepeat(parts[3]);
        alarm.setVibrate(Boolean.parseBoolean(parts[4]));
        alarm.setId(Long.parseLong(parts[5]));
        return alarm;
    }
}



